﻿using System;
using System.IO;
using System.Xml.Serialization;
using PSE.Deployment.Engine.Interfaces;
using Sinacor.Deployment.Setup.Configuration;
using Sinacor.Deployment.Setup.Install.DataBase.Steps;
using Sinacor.Deployment.Setup.Main;
using Sinacor.Deployment.Setup.Parameter;
using PSE.Deployment.SetupBase;
using PSE.Deployment.SetupBase.Groups;
using PSE.Deployment.SetupBase.Configuration;

namespace Sinacor.Deployment.Setup.Install.DataBase
{
    public class DataBaseGroup : BaseSetupGroup
    {
        public Group Group { get; set; }

        public DataBaseGroup(IStepGroup parent, Group group)
            : base(parent)
        {
            this.Id = group.Id;
            this.Description = group.Description;

            Group = group;

            SetupMainFactory.GetInstance<SinacorMain>().Configuration.Scripts = PSE.Deployment.Util.Serializer.Deserialize<DataBaseScripts>(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\DataBaseScripts.xml");

            if (SetupMainFactory.GetInstance<SinacorMain>().Parameter.DataBase.PermitedClearSinacorDataBase)
            {
                //Insere os passos para execução do script para as empresas selecionadas
                foreach (Company company in SetupMainFactory.GetInstance<SinacorMain>().Parameter.DataBase.Companies)
                {
                    this.Steps.Add(new Uninstall.DataBase.Steps.CompanyDropSchema(this, company, true));
                }

                //Insere os passos para execução do script para as empresas selecionadas
                foreach (Company company in SetupMainFactory.GetInstance<SinacorMain>().Parameter.DataBase.CompaniesNotAllowed)
                {
                    this.Steps.Add(new Uninstall.DataBase.Steps.CompanyDropSchema(this, company, true));
                }

                this.Steps.Add(new Uninstall.DataBase.Steps.SystemDropSchema(this, true));
            }

            this.Steps.Add(new SystemCreateSchema(this));
            this.Steps.Add(new SystemInsert(this));

            //Insere os passos para execução do script para as empresas selecionadas
            foreach (Company company in SetupMainFactory.GetInstance<SinacorMain>().Parameter.DataBase.Companies)
            {
                this.Steps.Add(new CompanyCreateSchema(this, company));
                this.Steps.Add(new CompanyInsert(this, company));
            }

            this.ExecutionFinished += new EventHandler<PSE.Deployment.Engine.CustomEventArgs.ExecutionFinishedEventArgs>(DataBaseGroup_ExecutionFinished);
            this.UndoFinished += new EventHandler<PSE.Deployment.Engine.CustomEventArgs.UndoFinishedEventArgs>(DataBaseGroup_UndoFinished);
        }

        private void DataBaseGroup_UndoFinished(object sender, PSE.Deployment.Engine.CustomEventArgs.UndoFinishedEventArgs e)
        {
            if (e.OriginalSource == this)
            {
                this.Group.Install = false;
                this.Group.Installed = false;
            }
        }

        private void DataBaseGroup_ExecutionFinished(object sender, PSE.Deployment.Engine.CustomEventArgs.ExecutionFinishedEventArgs e)
        {
            if (e.OriginalSource == this)
            {
                this.Group.Install = false;
                this.Group.Installed = false;
            }
        }

        public static string GetProgressDescription(string command)
        {
            if (!string.IsNullOrEmpty(command))
            {
                if (command.Trim().Length > 50)
                {
                    return command.Trim().Substring(0, 50).ToUpper() + "...";
                }
                else
                {
                    return command.Trim().ToUpper();
                }
            }

            return command;
        }
    }
}
